﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
//using Oracle.DataAccess.Client;
using System.Configuration;
using System.ComponentModel.DataAnnotations;
using trangbaocao.Models;
using System.Collections.ObjectModel;
using System.Web.Mvc;
using System.Data.OracleClient;
namespace trangbaocao.Models
{
    public class Sort_DeviceModel
    {
        public int id { get; set; }
        [Required(ErrorMessage = "Tên loại thiết bị không được để trống!")]
        public string name { get; set; }

        public int main_sort_device_id { get; set; }
        public SelectList chung_loai_tb_id { get; set; }

        public string mo_ta_dac_tinh { get; set; }
        public string ghi_chu { get; set; }        

        public List<Sort_DeviceModel> getLoaithietbi()
        {
            List<Sort_DeviceModel> sort_device_ = new List<Sort_DeviceModel>();
            OracleDataReader odr = General.Lib_GetDataReader("SELECT * FROM Sort_Device order by name");
            while (odr.Read())
            {
                sort_device_.Add(new Sort_DeviceModel { id = Convert.ToInt32(odr[0].ToString()), name = odr[1].ToString(), main_sort_device_id = Convert.ToInt32(odr[2].ToString()), mo_ta_dac_tinh = odr[3].ToString(), ghi_chu = odr[4].ToString() });
            }
            return sort_device_;
        }
        public List<Sort_DeviceModel> getLoaithietbiById(int id)
        {
            List<Sort_DeviceModel> sort_device_ = new List<Sort_DeviceModel>();
            OracleDataReader odr = General.Lib_GetDataReader("SELECT * FROM Sort_Device WHERE id=" + id);
            while (odr.Read())
            {
                sort_device_.Add(new Sort_DeviceModel { id = Convert.ToInt32(odr[0].ToString()), name = odr[1].ToString(), main_sort_device_id = Convert.ToInt32(odr[2].ToString()), mo_ta_dac_tinh = odr[3].ToString(), ghi_chu = odr[4].ToString() });
            }
            return sort_device_;
        }
        public void addLoaithietbi(Sort_DeviceModel sort_device)
        {
            string sql = "INSERT INTO Sort_Device";
            sql += " select nvl((select max(id)+1 from Sort_Device),1),'" + sort_device.name + "'," + sort_device.main_sort_device_id + ",'"+sort_device.mo_ta_dac_tinh+"','"+sort_device.ghi_chu+"' from dual";
            General.Lib_ExecuteNonQuery(sql);
        }

        public void updateLoaithietbi(Sort_DeviceModel sort_device)
        {
            string sql = "UPDATE Sort_Device SET name='" + sort_device.name + "',  main_sort_device_id="+sort_device.main_sort_device_id+",mo_ta_dac_tinh='"+sort_device.mo_ta_dac_tinh+"',ghi_chu='"+sort_device.ghi_chu+"' where id=" + sort_device.id;
            General.Lib_ExecuteNonQuery(sql);
        }
        public void deleteLoaithietbi(int id)
        {
            string sql = "DELETE FROM Sort_Device WHERE id=" + id;
            General.Lib_ExecuteNonQuery(sql);
        }
        public int checkExist(Sort_DeviceModel sort_device)
        {
            string sql = "SELECT * FROM Sort_Device WHERE name='" + sort_device .name + "'";
            return General.Lib_CheckExist(sql);
        }
        public int checkSubId(int id)
        {
            string sql = "SELECT * FROM Device WHERE sort_device_id='" + id + "'";
            return General.Lib_CheckExist(sql);
        }
    }
    public class Sort_DeviceAssociateModel
    {
        public int id { get; set; }
        public string name { get; set; }
        public string main_sort_device_name { get; set; }
        public string mo_ta_dac_tinh { get; set; }
        public string ghi_chu { get; set; }
        public List<Sort_DeviceAssociateModel> getLoaithietbiIndex()
        {
            List<Sort_DeviceAssociateModel> sort_device_ = new List<Sort_DeviceAssociateModel>();
            OracleDataReader odr = General.Lib_GetDataReader("SELECT a.id,a.name,b.name,a.mo_ta_dac_tinh,a.ghi_chu FROM Sort_Device a,Main_Sort_Device b where a.main_sort_device_id=b.id order by b.name,a.name");
            while (odr.Read())
            {
                sort_device_.Add(new Sort_DeviceAssociateModel { id = Convert.ToInt32(odr[0].ToString()), name = odr[1].ToString(), main_sort_device_name = odr[2].ToString(), mo_ta_dac_tinh = odr[3].ToString(), ghi_chu = odr[4].ToString() });
            }
            return sort_device_;
        }
    }
}